package com.zzty.servlet;

import com.zzty.dao.TeamExamDao;
import com.zzty.daoimpl.TeamExamDaoImpl;
import com.zzty.entity.TeamExam;
import com.zzty.entity.User;
import com.zzty.service.GroupService;
import com.zzty.service.TeamExamService;
import com.zzty.serviceimpl.GroupServiceImpl;
import com.zzty.serviceimpl.TeamExamServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "TeamExamServlet",urlPatterns = "/TeamExamServlet")
public class TeamExamServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if("setQuestion".equals(method)){
            setQuestion(request,response);
        }
        if("getTable".equals(method)){
            getTable(request,response);
        }
        if("checkDate".equals(method)){
            checkDate(request,response);
        }
        if("setWinners".equals(method)){
            setWinners(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TeamExamService tes=new TeamExamServiceImpl();
        String date=request.getParameter("date");
        List<Integer> winnerIds=tes.getWinnerIdByDate(date);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(winnerIds);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }

    protected void setWinners(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GroupService gs=new GroupServiceImpl();
        TeamExamService tes=new TeamExamServiceImpl();
        String date=request.getParameter("date");
        Integer groupId=Integer.parseInt(request.getParameter("winnerGroupId"));
        String name1=gs.getName1ById(groupId);
        String name2=gs.getName2ById(groupId);
        boolean result=tes.setWinnersByDate(date,name1,name2);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();

    }

    protected void setQuestion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String questionId=request.getParameter("tquestionId");
        String date=request.getParameter("tdate");
        User user=(User)request.getSession().getAttribute("user");
        Integer classId=user.getClassId();
        TeamExamDao td =new TeamExamDaoImpl();
        boolean result=td.insertQuestionId(questionId,classId,date);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void getTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        TeamExamService tes=new TeamExamServiceImpl();
        List<TeamExam> teamExams=null;
        String json=null;
        ObjectMapper mapper = new ObjectMapper();
        if(tes.getTeamExamToTable()!=null){
            teamExams=tes.getTeamExamToTable();
            json = mapper.writeValueAsString(teamExams);
        }
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void checkDate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String date=request.getParameter("date");
        TeamExamService tes=new TeamExamServiceImpl();
        boolean result=tes.checkDate(date);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(result);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
}
